<?php
/**
 * Created by PhpStorm.
 * User: 刘海平（785313152）
 * Date: 2019/4/4
 * Time: 11:33
 * Message: 对未来没概念，也不想有概念，脑子里只关心汤面会不会太咸！
 */

namespace app\index\controller;


use app\common\controller\IndexBase;
use think\Controller;

class Fileupload extends IndexBase
{
    public function upload()
    {
        $file = $this->request->file('file');
        //halt($file->getInfo()['name']);
        $maxWidth = input('max_width')?input('max_width'):1000;
        $maxHeight = input('max_height')?input('max_height'):1000;
        $path = input('path','images').'/';
        if($file){
            $upload_path = str_replace('\\', '/', ROOT_PATH . 'uploads/'.$path);

            $save_path   = '/uploads/'.$path;
            if(session('user_info')['userid']){
                $path_info = date('ymd').'/'.session('user_info')['userid'].date('His').'/';
            }
            $info        = $file->move($upload_path);//->validate(['size'=>104857600,'ext'=>'jpg,gif,png,jpeg,bmp'])//字节
//            $info        = $file->move($upload_path,$path_info.$file->getInfo()['name']);//->validate(['size'=>104857600,'ext'=>'jpg,gif,png,jpeg,bmp'])//字节
            if ($info) {
                $url = str_replace('\\', '/', $save_path . $info->getSaveName());
                //判断上传文件是图片
                if(function_exists("exif_imagetype")){  //exif_imagetype函数快得多,需exif扩展支持，虚拟主机无法修改配置文件
                    $is_ok = exif_imagetype('.'.$url);
                }else{
                    $is_ok = getimagesize('.'.$url);//使用getimagesize代替
                }
                if($is_ok === false){
                    $result = ['error' => 0,'url' =>$url];  //非图片时直接返回，无需处理
                }else{
                    $result = resizeImage('.'.$url,$maxWidth,$maxHeight);
                }
            } else {
                $result = ['error' => 1, 'message' => $file->getError()];
            }
            $result['name'] = $file->getInfo()['name'];
            exit(json_encode($result));//text/html，用于解决ie浏览器上传后弹出下载框
            //return json($result);//json数据
        }
    }

}